import json
import logging

from api.LoginAPI import LoginAPI
from config import BASE_DIR, username, password, code
from logging import handlers


def assert_utils(response, status_code, code, msg):
    assert status_code == response.status_code
    assert code == response.json().get("code")
    assert msg == response.json().get("msg")


def read_json_data(filename):
    file_path = BASE_DIR + "/data/" + filename
    with open(file_path, encoding="utf-8") as f:
        json_data = json.load(f)

    test_case_list = []
    for i in json_data:
        test_case_list.append(tuple(i.values()))
    logging.info(test_case_list)
    return test_case_list


def login_succeed(uesr=username, pwd=password, code=code):
    login_api = LoginAPI()

    response = login_api.get_inm_code()
    # logging.info(f"返回获取验证码的响应体数据{response.json()}")
    assert_utils(response, 200, 200, "操作成功")

    uuid = response.json().get("uuid")

    response = login_api.login(uesr, pwd, code, uuid)
    # logging.info(f"返回登录的响应体数据{response.json()}")
    assert_utils(response, 200, 200, "操作成功")

    token = response.json().get("token")
    logging.info(f'打印token的值{token}')
    return token


# 定义日志文件的初始化函数
def init_log_config():
    # 1、创建日志器对象
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    # 2、创建文件处理器和控制台处理器
    sh = logging.StreamHandler()
    file_path = BASE_DIR + "/log/KDTX.log"
    fh = logging.handlers.RotatingFileHandler(filename=file_path, backupCount=5, encoding='UTF-8')
    # 3、创建格式化器
    f = '%(asctime)s %(levelname)s [%(name)s] [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s'
    formatter = logging.Formatter(f)
    # 4、把格式化器添加到文件处理器和控制台处理器
    sh.setFormatter(formatter)
    fh.setFormatter(formatter)
    # 5、把处理器加到日志器
    logger.addHandler(sh)
    logger.addHandler(fh)


if __name__ == '__main__':
    init_log_config()
    logging.debug('1、这是一个debug日志')
    logging.info('2、这是一个info日志')
    logging.error('3、这是一个error日志')
    logging.info('4、这是一个debug日志')
    logging.info('5、这是一个info日志')
    logging.info('6、这是一个error日志')

